package com.xsjiong.hoofen;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v4.widget.CircularProgressDrawable;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

public class LoadingDialog extends Dialog {
	public LoadingDialog(Context cx) {
		super(cx, R.style.FullscreenDialog);
		setCanceledOnTouchOutside(false);
		setCancelable(false);
		Initialize();
	}

	@Override
	public void show() {
		super.show();
		Window w = getWindow();
		WindowManager.LayoutParams para = w.getAttributes();
		para.width = -1;
		para.height = -2;
		w.setAttributes(para);
	}

	private LinearLayoutCompat Root;
	private ProgressBar Content;
	private AppCompatTextView Message;

	private void Initialize() {
		Root = new LinearLayoutCompat(getContext());
		Root.setGravity(Gravity.CENTER);
		Root.setBackgroundDrawable(null);
		Root.setOrientation(LinearLayoutCompat.VERTICAL);
		Content = new ProgressBar(getContext());
		CircularProgressDrawable drawable = new CircularProgressDrawable(getContext());
		drawable.setStrokeWidth(15);
		drawable.setBackgroundColor(Color.TRANSPARENT);
		drawable.setColorSchemeColors(G.REFRESH_COLORS);
		Content.setIndeterminateDrawable(drawable);
		Root.addView(Content);
		Message = new AppCompatTextView(getContext());
		Message.setTextColor(Color.WHITE);
		LinearLayoutCompat.LayoutParams params = new LinearLayoutCompat.LayoutParams(-2, -2);
		params.topMargin = UI.dp2px(10);
		Root.addView(Message, params);
		Message.setVisibility(View.GONE);
		setContentView(Root);
	}

	public LoadingDialog setMessage(CharSequence cs) {
		Message.setText(cs);
		Message.setVisibility(cs == null ? View.GONE : View.VISIBLE);
		return this;
	}
}
